//
//  SRModifyPasswordViewController.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/5/1.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRModifyPasswordViewController.h"
#import "UIButton+FZGreenButton.h"
#import "FCUtil.h"
#import "SRCommonConstants.h"
#import "AppDelegate.h"

@interface SRModifyPasswordViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *originPwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *nowPwdTextField;

@property (weak, nonatomic) IBOutlet UIButton *doneButton;


@end

@implementation SRModifyPasswordViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"修改密码";
    [self setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupView {
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    self.view.backgroundColor = css.color_2;
    [[UITextField appearance] setTintColor:css.color_1];
    self.originPwdTextField.delegate = self;
    self.nowPwdTextField.delegate = self;
    self.originPwdTextField.font = self.nowPwdTextField.font = css.fontOfH6;
    self.originPwdTextField.textColor = self.nowPwdTextField.textColor = css.color_6;
    
    [self.doneButton applyEllipseGreenBackgroundColor];
    [self.doneButton setEnabled:NO];

    [self.doneButton addTarget:self action:@selector(onDoneButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.originPwdTextField];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:self.nowPwdTextField];
}

- (void)onDoneButton:(id)sender {
    if ([self checkViladInput]) {
        [self resetPassword];
    }
}

- (void)resetPassword {
    NSString *password = [self stringTrimWhiteSpaceWithSourceString:self.originPwdTextField.text];
    NSString *nowpassword = [self stringTrimWhiteSpaceWithSourceString:self.nowPwdTextField.text];
    
    NSDictionary *parameters = [@{} mutableCopy];
//    [parameters setValue:[FCUtil md5HexString:password] forKey:@"password"];
    [parameters setValue:password forKey:@"password"];

//    [parameters setValue:[FCUtil md5HexString:nowpassword] forKey:@"newpassword"];
    [parameters setValue:nowpassword forKey:@"newpassword"];

    
    
    WEAKSELF
    [self startProgressHUD];
    NSString *urlString = [[SRAPICreater sharedInstance] APIWithName:@"user_changePassword"];
    [[SRNetWorkManager sharedInstance] POST:urlString needCache:NO parameters:parameters responseClass:nil success:^(NSInteger statusCode, NSString *message, id dataObject) {
        [weakSelf stopProgressHUD];
        if (statusCode == kFZRequestStatusCodeSuccess) {
            [weakSelf showHUDHintWithText:@"修改成功，请重新登录"];
            [self performSelector:@selector(logout) withObject:nil afterDelay:1];
        } else {
            if (message) {
                [weakSelf showHUDHintWithText:message];
            } else {
                [weakSelf showHUDHintWithText:@"修改失败"];
            }
        }
        
    } failure:^(id responseObject, NSError *error) {
        [weakSelf stopProgressHUD];
        [weakSelf showHUDHintWithText:@"修改失败"];
    }];
}



-(void)logout {
    
    [[SRLoginManager sharedManager] logout];
    [[NSNotificationCenter defaultCenter] postNotificationName:SRUserLogoutDoneNotification object:nil];

    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate logoutAndRelogin:nil whetherEnterLoginVC:YES];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)textFieldTextDidChangeNotification:(NSNotification *)notification {
    if (notification.object == self.nowPwdTextField) {
//        FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
        NSString *phone = [self stringTrimWhiteSpaceWithSourceString:self.nowPwdTextField.text];
        if (phone) {
          
        } else {
            
        }
    } else if (notification.object == self.originPwdTextField) {
            //        NSLog(@"password");
    }
    
    if ([self stringTrimWhiteSpaceWithSourceString:self.originPwdTextField.text] && [self stringTrimWhiteSpaceWithSourceString:self.nowPwdTextField.text]) {
        [self.doneButton setEnabled:YES];
    } else {
        [self.doneButton setEnabled:NO];
    }
    
}

- (BOOL)checkViladInput {
    [self.view endEditing:YES];
    NSString *originPassword = [self stringTrimWhiteSpaceWithSourceString:self.nowPwdTextField.text];

    if (!originPassword || [originPassword length] <= 0) {
        [self showHUDHintWithText:@"请输入旧密码"];
        return NO;
    }
    
    NSString *password = [self stringTrimWhiteSpaceWithSourceString:self.nowPwdTextField.text];
    if (!password || [password length] < 6) {
        [self showHUDHintWithText:@"密码最少6位"];
        return NO;
    }
    
    if (![FCUtil isValidatePassword:password]) {
        [self showHUDHintWithText:@"密码太简单"];
        return NO;
    }

    
    return YES;
}

- (NSString *)stringTrimWhiteSpaceWithSourceString:(NSString *)sourceString {
    NSCharacterSet *whitespace = [NSCharacterSet  whitespaceAndNewlineCharacterSet];
    NSString *resultString = [sourceString stringByTrimmingCharactersInSet:whitespace];
    if (resultString && resultString.length > 0) {
        return resultString;
    }
    
    return nil;
}

@end
